//获取超链接dom 元素
const registerA = document.querySelector(".register a"); //注册页面超链接
const loginA = document.querySelector(".login a"); //登录页面超链接
const loginBox = document.querySelector(".login"); //登录页面的盒子
const registerBox = document.querySelector(".register"); //注册页面的盒子

//给登录超链接绑定点击事件
loginA.addEventListener("click", function (e) {
  console.log("登录");
  loginBox.style.display = "none";
  registerBox.style.display = "block";
});

//给注册超链接绑定点击事件
registerA.addEventListener("click", function (e) {
  console.log("注册");
  loginBox.style.display = "block";
  registerBox.style.display = "none";
});

//注册页面

const register = document.querySelector(".register"); //注册页面
const registerForm = register.querySelector("form"); //注册表单
registerForm.addEventListener("submit", async function (e) {
  e.preventDefault();
  console.log("点击注册");
  //获取表单数据
  const data = serialize(registerForm, { hash: true, empty: true });
  console.log(data);
  //获取数据
  const result = await axios({
    url: "/api/register",
    method: "post",
    data,
  });
  if (result.data.code === 0) {
    registerForm.reset(); //重置表单
  }
  console.log(result);
});

//登录

const login = document.querySelector(".login"); //注册页面
const loginForm = login.querySelector("form"); //注册表单
loginForm.addEventListener("submit", async function (e) {
  e.preventDefault();
  const data = serialize(loginForm, { hash: true, empty: true });
  console.log(data);
  //   console.log("点击登录");
  //获取数据
  const result = await axios({
    url: "/api/login",
    method: "post",
    data,
  });
  console.log(result);
  if (result.data.code === 0) {
    toastr.success("登录成功");
    // console.log(result);
    localStorage.setItem("cms1", result.data.token);
    location.href = "index.html";
  }
});
